package cn.edu.aust.myexpri5app.UI;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.aust.myexpri5app.R;
import cn.edu.aust.myexpri5app.model.UserInfo;
import cn.edu.aust.myexpri5app.utils.FileTools;
import cn.edu.aust.myexpri5app.utils.JsonTools;
import cn.edu.aust.myexpri5app.utils.MyRecyclerAdapter;

public class ShowInfoActivity extends AppCompatActivity {
    RecyclerView rcUserInfo=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_info);

        rcUserInfo=(RecyclerView)findViewById(R.id.rcv_userInfo);


        String origData=null;
        try {
            origData= FileTools.readDataFromInner(getApplicationContext());
        } catch (IOException e) {
            e.printStackTrace();
        }

        final List<UserInfo> userList = JsonTools.jsonToObjs(origData);

        //设置RecyclerView参数
        rcUserInfo.setHasFixedSize(true);
        LinearLayoutManager myLayoutManager=new LinearLayoutManager(getApplicationContext());
        myLayoutManager.setOrientation(RecyclerView.VERTICAL);
        rcUserInfo.setLayoutManager(myLayoutManager);
        MyRecyclerAdapter rclAdapter=new MyRecyclerAdapter(getApplicationContext(),userList);
        rclAdapter.setOnItemClickListener(new MyRecyclerAdapter.ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getApplicationContext(),
                        "您点击了用户："+userList.get(position).getUsername()+"位置："+position,
                        Toast.LENGTH_LONG).show();
            }
        });
        rcUserInfo.setAdapter(rclAdapter);
    }
}